<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type {
  KnowledgeList,
  KnowledgeType,
  KnowledgeParams
} from '@/types/consult'
import { getKnowledgePage } from '@/service/consult'

// 1.定义finished和loading数据 list数据
// 2.获取数据，加载中，就修改loading为true；加载成功，就修改loading为false

const list = ref<KnowledgeList>([]) // 文章列表渲染
const finished = ref(false)
const loading = ref(false)
// 组件props
const props = defineProps<{
  type: KnowledgeType
}>()
const searchParams = ref<KnowledgeParams>({
  pageSize: 10,
  current: 1,
  type: props.type
})
const onLoad = async () => {
  try {
    const res = await getKnowledgePage(searchParams.value)
    list.value.push(...res.data.rows)
    console.log(res)
    if (searchParams.value.current >= res.data.pageTotal) {
      finished.value = true
    } else {
      searchParams.value.current++
    }
    loading.value = false
  } catch (error) {
    console.log(error)
    loading.value = false
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card
        :item="item"
        v-for="(item, index) in list"
        :key="index"
      ></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
